import akka.actor.*;

/**
 * 
 * @author Pietro Bua & Rohmin Mirza
 *
 */
public class Customer extends UntypedActor
{
	private ActorRef secretary;
	/**
	 * 
	 */
	public Customer(ActorRef s)
	{
		secretary = s;
	}
	
	/**
	 * 
	 */
	@Override
	public void preStart() throws InterruptedException
	{
		System.out.println( ">" + getSelf().path().name() + " started and getting ready to meet");
		justLive();
		secretary.tell(Message.REQUEST_MEETING_CUS, getSelf());
		System.out.println(getSelf().path().name() + " ready to meet and waiting for invite");
	}
	
	/**
	 * 
	 */
	@Override
	public void onReceive(Object message) throws Exception
	{
		if( message instanceof Message )
		{
			Message m = (Message) message;
			switch(m)
			{
				case INVITE_TRAVEL:
					justLive();
					System.out.println(getSelf().path().name() + " arrived to company");
					getSender().tell(Message.CUS_ARRIVE, getSelf());
					break;
				case INVITE_MEETING:
					justLive();
					System.out.println(getSelf().path().name() + "  in the meeting room");
					getSender().tell(Message.MEETING_ACCEPT, getSelf());
					break;
				case MEETING_END:
					justLive();
					break;
				default:unhandled(m);
			}
		}
		else
		{
			System.out.println("ERROR: Developer->onReceive - message != instanceof Message");
		}
	}
	
	/**
	 * @throws InterruptedException 
	 * 
	 */
	public void postStop() throws InterruptedException
	{
		System.out.println( "<" + getSelf().path().name() + " exiting" );
	}

	/**
	 * Makes a random time in which the developer is programming
	 * @throws InterruptedException 
	 */
	private void justLive() throws InterruptedException
	{
        int time = (int)(Math.random() * 1000);
		System.out.println(getSelf().path().name() + " living " + time + "ms");
		Thread.sleep( time );
	}
	
	/**
	 * 
	 * @param secretary
	 * @return
	 */
	public static Props prop(ActorRef secretary)
	{
		return Props.create(Customer.class, secretary);
	}
}
